package com.kamistoat.meimeistore.product.aliyunVideo.service.impl;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.kamistoat.meimeistore.product.aliyunVideo.InitObject;
import com.kamistoat.meimeistore.product.aliyunVideo.service.GetVideoInfoAuthService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class GetVideoInfoAuthServiceImpl implements GetVideoInfoAuthService {

    /**
     * 从nacos的配置文件中导入字段赋值给本地参数
     */
    @Value("${spring.cloud.alicloud.access-key}")
    private String accessId;
    @Value("${spring.cloud.alicloud.secret-key}")
    private String secret;

    /**
     * 获取视频播放地址
     *
     * @param videoId 视频在阿里云中的唯一Id
     * @return 库类 GetPlayInfoResponse
     */
    @Override
    public GetPlayInfoResponse getVideoInfo(String videoId) throws ClientException {
        // 初始化视频点播服务端
        DefaultAcsClient defaultAcsClient = InitObject.initVodClient(accessId, secret);

        // 新建库类 视频地址请求类
        GetPlayInfoRequest getPlayInfoRequest = new GetPlayInfoRequest();

        // 设置视频Id
        getPlayInfoRequest.setVideoId(videoId);

        // 使用服务端获取视频地址响应类
        GetPlayInfoResponse acsResponse = defaultAcsClient.getAcsResponse(getPlayInfoRequest);

        return acsResponse;
    }

    /**
     * 获取视频播放凭证
     *
     * @param videoId 视频在阿里云中的唯一ID
     * @return 库类 GetVideoPlayAuthResponse
     */
    @Override
    public GetVideoPlayAuthResponse getVideoAuth(String videoId) throws ClientException {
        DefaultAcsClient defaultAcsClient = InitObject.initVodClient(accessId, secret);
        GetVideoPlayAuthRequest getVideoPlayAuthRequest = new GetVideoPlayAuthRequest();
        getVideoPlayAuthRequest.setVideoId(videoId);
        GetVideoPlayAuthResponse acsResponse = defaultAcsClient.getAcsResponse(getVideoPlayAuthRequest);
        return acsResponse;
    }
}
